package com.ckerchina.admin.ckadmin.controller.console;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.ckerchina.admin.ckadmin.dao.AgentUserDao;
import com.zhuzi.controller.dto.ResponseData;
import com.zhuzi.exception.ErrorCode;
import com.zhuzi.utils.JsonMapper;


@Controller
public class OrderController{

	protected Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private AgentUserDao industryInfoDao;


	
	@RequestMapping(value = "/uploadheadimg")
	public @ResponseBody String upload(
			@RequestParam(value = "photo-path", required = false) MultipartFile file,
			@RequestParam(value = "id", required = false) String id) {
		String fileName = saveFile(file);
		logger.info("fileName {}",fileName);
		
		logger.info("id: {}",id);		
		ResponseData rd = new ResponseData();
	 
		
		rd.setValue(ErrorCode.OK.memo);
		rd.setCode(ErrorCode.OK.value);
		logger.info(JsonMapper.toJson(rd));
		return JsonMapper.toJson(rd);
	}
	
	private String saveFile(MultipartFile file) {
		String path = this.getClass().getResource("/../../").toString();
		path = path.replace("file:/", "");
		logger.info("path.indexOf(:)==0:"
				+ String.valueOf(path.indexOf(":") == 0));
		if (path.indexOf(":") == -1) {
			path = "/" + path;
			logger.info("path2:" + path);
		}
		path = path.replace("zhuzi", "images");
		logger.info("path:" + path);

		BufferedImage input;
		String allPath = "";
		String name = "";
		String fileName = file.getOriginalFilename();
		String format = fileName.substring(fileName.indexOf(".") + 1);
		logger.info("file.getName() {} ", file.getName());
		logger.info("file.getOriginalFilename() {} ",
				file.getOriginalFilename());
		try {
			CommonsMultipartFile cf = (CommonsMultipartFile) file;
			DiskFileItem fi = (DiskFileItem) cf.getFileItem();
			File f1 = fi.getStoreLocation();
			input = ImageIO.read(f1);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyymmddHHmmss");
			name = sdf.format(new Date());
			allPath = path + name + "." + format;
			logger.info("allpath {}", allPath);
			File f = new File(allPath);
			ImageIO.write(input, format, f);
		} catch (IOException e) {
			logger.info("errMsg: {}", e.getMessage());
			// e.printStackTrace();
		}

		return name + "."+format;
	}
}
